﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.Skins;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList;
using DevExpress.Utils.Design;

namespace DevExpress.MailClient.Win {
    public partial class ucMailTree : BaseControl {
        public event DataSourceChangedEventHandler DataSourceChanged;
        public event MouseEventHandler ShowMenu;
        bool allowDataSourceChanged = false;
        public ucMailTree() {
            InitializeComponent();
            InitData();
            treeList1.RowHeight = Math.Max(Convert.ToInt32(treeList1.Font.GetHeight()), icFolders.ImageSize.Height) + 5;
        }
        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);
            if(!DesignTimeTools.IsDesignMode)
                treeList1.FocusedNode = treeList1.Nodes[0].Nodes[0];
        }
        void InitData() {
           
        }

        void CreateMessagesList(TreeListNodes nodes) {
            foreach(TreeListNode node in nodes) {
                CreateMessagesForNode(node);
                CreateMessagesList(node.Nodes);
            }
        }
        void CreateMessagesForNode(TreeListNode node) {
          
        }
        protected override void LookAndFeelStyleChanged() {
            
        }
        private void treeList1_FocusedNodeChanged(object sender, DevExpress.XtraTreeList.FocusedNodeChangedEventArgs e) {
            
        }
        void RaiseDataSourceChanged(TreeListNode node) {
           
        }
       
        private void treeList1_CustomDrawNodeCell(object sender, CustomDrawNodeCellEventArgs e) {
            if(DesignTimeTools.IsDesignMode) return;
            if(e.Column == colName) {
                string textColor = GetHtmlTextColor(treeList1.FocusedNode.Equals(e.Node));
                object textValue = e.Node.GetValue(colName);
                e.CellText = string.Format("<Color={1}>{0}", textValue, textColor);
                if(e.Node.ParentNode == null || !(DataHelper.ShowAllMessageCount || DataHelper.ShowUnreadMessageCount)) return;
                List<Message> list = e.Node.GetValue(colData) as List<Message>;
                int unread = GetUnreadMessagesCount(list);
                if(unread > 0 && DataHelper.ShowUnreadMessageCount) {
                    if(DataHelper.ShowAllMessageCount)
                        e.CellText = string.Format("<Color={5}><b>{0}</b><Size=-1><Color={2}> [{1}/<Color={4}>{3}<Color={2}>]", textValue, unread, ColorHelper.HtmlQuestionColor, list.Count, ColorHelper.HtmlWarningColor, textColor);
                    else
                        e.CellText = string.Format("<Color={3}><b>{0}</b><Size=-1><Color={2}> [{1}]", textValue, unread, ColorHelper.HtmlQuestionColor, textColor);
                } else {
                    if(DataHelper.ShowAllMessageCount && list.Count > 0)
                        e.CellText = string.Format("<Color={3}>{0}<Size=-1><Color={2}> [{1}]", textValue, list.Count, ColorHelper.HtmlWarningColor, textColor);
                }
            }
        }
        static string GetHtmlTextColor(bool focused) {
            if(focused) 
                return ColorHelper.HtmlHighlightTextColor;
            return AllowControlTextColor ? ColorHelper.HtmlControlTextColor : ColorHelper.HtmlControlTextColor2;
        }
        static bool AllowControlTextColor {
            get {
                return DevExpress.LookAndFeel.UserLookAndFeel.Default.ActiveSkinName != "Glass Oceans";
            }
        }
        int GetUnreadMessagesCount(List<Message> list) {
            return list.Count(p => p.IsUnread);
        }
        internal void RefreshTreeList() {
            treeList1.LayoutChanged();
        }
        private void treeList1_MouseDown(object sender, MouseEventArgs e) {
            if(e.Button == MouseButtons.Right && ShowMenu != null) ShowMenu(sender, e); 
        }

        internal void UpdateTreeViewMessages() {
            CreateMessagesList(treeList1.Nodes);
            RefreshTreeList();
            RaiseDataSourceChanged(treeList1.FocusedNode);
        }
    }
}
